package com.yic.module.base.controller.admin.itemCategory;

import com.yic.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.itemCategory.vo.*;
import com.yic.module.base.dal.dataobject.itemCategory.ItemCategoryDO;
import com.yic.module.base.convert.itemCategory.ItemCategoryConvert;
import com.yic.module.base.service.itemCategory.ItemCategoryService;

@Tag(name = "管理后台 - 产品分类")
@RestController
@RequestMapping("/base/item-category")
@Validated
public class ItemCategoryController {

    @Resource
    private ItemCategoryService itemCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建产品分类")
    @PreAuthorize("@ss.hasPermission('base:item-category:create')")
    public CommonResult<Long> createItemCategory(@Valid @RequestBody ItemCategoryCreateReqVO createReqVO) {
        return success(itemCategoryService.createItemCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品分类")
    @PreAuthorize("@ss.hasPermission('base:item-category:update')")
    public CommonResult<Boolean> updateItemCategory(@Valid @RequestBody ItemCategoryUpdateReqVO updateReqVO) {
        itemCategoryService.updateItemCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:item-category:delete')")
    public CommonResult<Boolean> deleteItemCategory(@RequestParam("id") Long id) {
        itemCategoryService.deleteItemCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:item-category:query')")
    public CommonResult<ItemCategoryRespVO> getItemCategory(@RequestParam("id") Long id) {
        ItemCategoryDO itemCategory = itemCategoryService.getItemCategory(id);
        return success(ItemCategoryConvert.INSTANCE.convert(itemCategory));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品分类列表")
    @PreAuthorize("@ss.hasPermission('base:item-category:query')")
    public CommonResult<List<ItemCategoryRespVO>> getItemCategoryList(ItemCategoryReqVO reqVO) {
        List<ItemCategoryDO> list = itemCategoryService.getItemCategoryList(reqVO);
        return success(ItemCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取产品分类精简信息列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<ItemCategorySimpleRespVO>> getSimpleItemCategoryList() {
        // 获得分类列表，只要开启状态的
        ItemCategoryReqVO reqVO = new ItemCategoryReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<ItemCategoryDO> list = itemCategoryService.getItemCategoryList(reqVO);
        // 返回给前端
        return success(ItemCategoryConvert.INSTANCE.convertList03(list));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品分类 Excel")
    @PreAuthorize("@ss.hasPermission('base:item-category:export')")
    @OperateLog(type = EXPORT)
    public void exportItemCategoryExcel(@Valid ItemCategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemCategoryDO> list = itemCategoryService.getItemCategoryList(exportReqVO);
        // 导出 Excel
        List<ItemCategoryExcelVO> datas = ItemCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品分类.xls", "数据", ItemCategoryExcelVO.class, datas);
    }

}
